package com.pigotech.ptwo.UI.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pigotech.ptwo.R;
import com.pigotech.ptwo.adapter.MyRecyclerAdapter;
import com.pigotech.ptwo.constant.Constant;
import com.pigotech.ptwo.entity.NormalVideo;
import com.pigotech.ptwo.interfaces.CallBack;
import com.pigotech.ptwo.socket.CommandSocket;
import com.pigotech.ptwo.widget.MyRecyclerView;

import java.util.Collections;
import java.util.List;

/**
 * Created by ghb on 2018/3/29.
 */

public class EmergencyVideoActivity extends Activity implements CallBack, View.OnClickListener {

    private ImageView iv_group;
    private MyRecyclerView recyclerview;
    private List<NormalVideo> dataList = null;
    private TextView tv_name;
    private String video_name = "";
    private MyRecyclerAdapter adapter;
    public int statusBarHeight = -1;//系统状态栏高度
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_emergency_video);
        getStatusBarHeight();
        setStatusBar();
        initView();
        initData();

    }
    /**
     * 获取系统状态栏高度
     */
    private void getStatusBarHeight() {
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
    }
    /**
     *4.4系统以上开启沉浸式后应用标题防止与系统标题重叠
     */
    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            findViewById(R.id.title_view).setVisibility(View.VISIBLE);
            findViewById(R.id.title_view).getLayoutParams().height = statusBarHeight;
//            findViewById(R.id.title_view).setVisibility(View.VISIBLE);
//            findViewById(R.id.title_view).getLayoutParams().height = statusBarHeight;
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明底部导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    private void initView() {
        iv_group = (ImageView) findViewById(R.id.iv_group);
        iv_group.setOnClickListener(this);
        recyclerview = (MyRecyclerView) findViewById(R.id.recyclerview);
        tv_name = (TextView) findViewById(R.id.tv_name);
        // 设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(layoutManager);
    }

    private void initData() {
        CommandSocket.getInstance().getNormalVideo();
        CommandSocket.getInstance().setCallBack(this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                CommandSocket.getInstance().doCallBackMethod();
            }
        }, 1000);
    }

    @Override
    public void doSomething(List<NormalVideo> list) {
        Collections.reverse(list);
        dataList = list;
        adapter = new MyRecyclerAdapter(this, dataList);
        recyclerview.setAdapter(adapter);
        recyclerview.setOnItemScrollChangeListener(new MyRecyclerView.onItemScrollChangeListener() {
            @Override
            public void onChange(View view, int position) {
                video_name= dataList.get(position).getFilename();
                tv_name.setText(dataList.get(position).getFilename());
                Glide.with(EmergencyVideoActivity.this).load("http://" + Constant.IP_ADDRESS + Constant.NORMAL_VIDEO_TUMB_DIRECTORY + dataList.get(position).getFilename().replace("A.MP4","T.JPG")).into(iv_group);
            }
        });
        adapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                video_name= dataList.get(position).getFilename();
                tv_name.setText(dataList.get(position).getFilename());
                Glide.with(EmergencyVideoActivity.this).load("http://" + Constant.IP_ADDRESS + Constant.NORMAL_VIDEO_TUMB_DIRECTORY + dataList.get(position).getFilename().replace("A.MP4","T.JPG")).into(iv_group);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_group:
                Intent intent_play_back = new Intent(this, PlayBackActivity.class);
                intent_play_back.putExtra("video_name", video_name);
                startActivity(intent_play_back);
                break;
        }


    }
}
